﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinFormsGraphicsDevice;
using Microsoft.Xna.Framework;
using Projet_Synthese;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Windows.Forms;

namespace Projet_Synthese_Editeur
{
    public class GameControl : GraphicsDeviceControl
    {
        public Tangram MonTangram 
        {
            get
            {
                return MainForm.Partie.Tangram;
            }
            set
            {
                MainForm.Partie.Tangram = value;
            }
        }
        public ContentManager Content { get; set; }
        private SpriteBatch Sprite;
        private int _selection = -1;

        public int Selection
        {
            get { return _selection; }
            set { _selection = value; }
        }
        public Texture2D[] Textures;
        public Form1 MainForm;
        public GameControl()
            : base()
        {
            MainForm = null;
            Content = new ContentManager(Services, "Textures");
        }
        public GameControl(Form1 form):base()
        {
            MainForm = form;
            Content = new ContentManager(Services, "Textures");
        }
        protected override void Initialize()
        {
            Sprite = new SpriteBatch(GraphicsDevice);
            Application.Idle += delegate { Invalidate(); };
            LoadContent();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
            {

                Rectangle rectangle = new Rectangle(e.X, e.Y, 8, 8);
                for (int i = 0; i < 7; i++)
                {
                    if (MonTangram[i] != null && MonTangram[i].RectangleDestination.Intersects(rectangle))
                    {
                        if (MonTangram[_selection] != null)
                            MonTangram[_selection].EstSelectionné = false;
                        MonTangram[i].EstSelectionné = true;
                        _selection = i;
                        break;
                    }
                }
                if (e.Button == MouseButtons.Right && MonTangram[_selection] != null)
                {
                    MainForm.contextMenuStrip1.Show(e.X,e.Y);
                    MainForm.UpdateOptionStatuts(MonTangram[_selection]);
                }
            }
            
            base.OnMouseClick(e);
        }
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            if (MonTangram[_selection] != null)
            {
                if (e.Delta > 0)
                    MonTangram[_selection].RotationForme(Partie.AngleRatation);
                else MonTangram[_selection].RotationForme(-Partie.AngleRatation);
            }
            base.OnMouseWheel(e);
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (MonTangram[_selection] != null && e.Button == MouseButtons.Left)
            {
                MonTangram[_selection].RectangleDestination.X = e.X;
                MonTangram[_selection].RectangleDestination.Y = e.Y;
            }
            base.OnMouseMove(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (MonTangram[_selection] != null)
                    MonTangram[_selection].EstSelectionné = false;
                _selection = -1;
            }
            base.OnMouseUp(e);
        }
        public virtual void LoadContent()
        {
            Textures = new Texture2D[20];
            Textures[0] = Content.Load<Texture2D>("Base/Carre");
            Textures[1] = Content.Load<Texture2D>("Base/Parallelogramme");
            Textures[2] = Content.Load<Texture2D>("Base/TriangleGrand");
            Textures[3] = Content.Load<Texture2D>("Base/TriangleMoyen");
            Textures[4] = Content.Load<Texture2D>("Base/TrianglePetit");
            Textures[5] = Content.Load<Texture2D>("Base/CarreSelect");
            Textures[6] = Content.Load<Texture2D>("Base/ParallelogrammeSelect");
            Textures[7] = Content.Load<Texture2D>("Base/TriangleGrandSelect");
            Textures[8] = Content.Load<Texture2D>("Base/TriangleMoyenSelect");
            Textures[9] = Content.Load<Texture2D>("Base/TrianglePetitSelect");
            Textures[10] = Content.Load<Texture2D>("Base/CarreVerrouille");
            Textures[11] = Content.Load<Texture2D>("Base/ParallelogrammeVerrouille");
            Textures[12] = Content.Load<Texture2D>("Base/TriangleGrandVerrouille");
            Textures[13] = Content.Load<Texture2D>("Base/TriangleMoyenVerrouille");
            Textures[14] = Content.Load<Texture2D>("Base/TrianglePetitVerrouille");
            Textures[15] = Content.Load<Texture2D>("Base/CarrePlein");
            Textures[16] = Content.Load<Texture2D>("Base/ParallelogrammePlein");
            Textures[17] = Content.Load<Texture2D>("Base/TriangleGrandPlein");
            Textures[18] = Content.Load<Texture2D>("Base/TriangleMoyenPlein");
            Textures[19] = Content.Load<Texture2D>("Base/TrianglePetitPlein");

            for (int i = 1; i < 5; i++)
            {
                Textures[(i * 5) - IdTexture.Carre].Tag = "Carre";
                Textures[(i * 5) - IdTexture.Parallelogramme].Tag = "Parallelogramme";
                Textures[(i * 5) - IdTexture.TriangleGrand].Tag = "TriangleGrand";
                Textures[(i * 5) - IdTexture.TriangleMoyen].Tag = "TriangleMoyen";
                Textures[(i * 5) - IdTexture.TrianglePetit].Tag = "TrianglePetit";
            }
            MonTangram = new Tangram(Textures);
            MainForm.UpdateVue();
        }
        protected override void Draw()
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            Sprite.Begin();
                MonTangram.DrawFormes(Sprite);
            Sprite.End();
        }
    }
}
